import time
import threading
from config import nacos_client  # 直接导入全局 nacos_client

NACOS_SERVICE_NAME = "flask-service"
NACOS_PORT = 5000

# 注册服务
def register_service():
    nacos_client.add_naming_instance(NACOS_SERVICE_NAME, "127.0.0.1", NACOS_PORT)

# 发送心跳
def keep_registered():
    while True:
        try:
            nacos_client.send_heartbeat(NACOS_SERVICE_NAME, "127.0.0.1", NACOS_PORT)
        except Exception as e:
            print(f"❌ 发送心跳失败: {e}")
        time.sleep(10)

# 启动服务注册 & 维持心跳
register_service()
threading.Thread(target=keep_registered, daemon=True).start()